<template>
  <div>
  <div class="outmain ba_f formwidth">
    <div class="shuju_title mar_b20">
      <div class="shuju_title_text">
        <span>抖音小程序设置</span>
      </div>
    </div>
    <div>
      <el-form ref="form" :model="form" label-width="180px" class="mar_t20">
        <el-form-item label="当前小程序uniacid" :required="true">
          <el-input v-model="form.uniacid" disabled></el-input>
        </el-form-item>
        <el-form-item label="APP ID" :required="true" prop="appKey">
          <el-input v-model="form.appKey" placeholder="请输入小程序APP ID"></el-input>
        </el-form-item>
        <el-form-item label="AppSecret" :required="true" prop="appSecret">
          <el-input v-model="form.appSecret" placeholder="请输入小程序AppSecret" :type="passw">
            <i slot="suffix" :class="icon" @click="showPass"></i>
          </el-input>
        </el-form-item>
        <div class="shuju_title mar_b20">
          <div class="shuju_title_text">
            <span>担保交易设置</span>
          </div>
        </div>
        <el-form-item label="SALT" :required="true" prop="salt">
          <el-input v-model="form.salt" placeholder="请输入担保交易salt" :type="passw2">
            <i slot="suffix" :class="icon2" @click="showPass2"></i>
          </el-input>
          <div class="yb_margin lh16">获取方式：字节小程序开发者平台-功能管理-支付-担保交易->担保交易设置->salt</div>
        </el-form-item>
        <el-form-item label="Token" :required="true" prop="token">
          <el-input v-model="form.token" placeholder="请输入担保交易token"></el-input>
          <div class="yb_margin lh16">获取方式：字节小程序开发者平台-功能管理-支付-担保交易->担保交易设置->token</div>
        </el-form-item>
      </el-form>
    </div>
  </div>
    <div class="mar_t20 ba_f pad_20 t_c">
      <el-button type="primary" @click="onSubmit">保存</el-button>
    </div>
  </div>
</template>
<script>
import {getConfig, postConfig} from "@/api/setup";

  export default {
    created() {
      this.init();
    },
    data () {
      return {
        activeName:'1',
        form: {
          uniacid: '',
          appKey: '',
          appSecret: '',
          salt: '',
          token: '',
        },
        passw: 'password',
        icon: 'el-input__icon el-icon-view',
        passw2: 'password',
        icon2: 'el-input__icon el-icon-view',
      }
    },
    methods: {
      async init() {
        this.bus.$emit('loading', true);
        this.formData();
      },
      showPass() {
        if (this.passw == 'text') {
          this.passw = 'password';
          this.icon = 'el-input__icon el-icon-view';
        } else {
          this.passw = 'text';
          this.icon = 'el-input__icon el-icon-unlock';
        }
      },
      showPass2() {
        if (this.passw2 == 'text') {
          this.passw2 = 'password';
          this.icon2 = 'el-input__icon el-icon-view';
        } else {
          this.passw2 = 'text';
          this.icon2 = 'el-input__icon el-icon-unlock';
        }
      },
      async formData() {
        const { data } = await getConfig({ ident: 'toutiao' });
        this.form2 = this.form;
        this.form = Object.assign(this.form2, data);
        this.bus.$emit('loading', false);
      },
      async onSubmit() {
          this.$refs['form'].validate(async (valid) => {
            if (valid) {
              this.form.ident = 'toutiao';
              this.form.identName = '字节跳动小程序设置';
              const { msg } = await postConfig(this.form);
              this.$baseMessage(msg, 'success');
              this.init();
            } else {
              return false;
            }
          });
      },
    }
  }
</script>